package com.lanlian.cayman.activity;

import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.lanlian.cayman.R;
import com.lanlian.commonlib.base.BaseActivity;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/11/7 9:55
 *     desc   : webview
 *     version: 1.0
 * </pre>
 */
public class WebviewActivity extends BaseActivity {

    private WebView mWebView;
    private String mUrl;
    private String mTitle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web, true);
        initData();
        initView();
        setListeners();
        showLoadingDialog();
        //支持javascript
        mWebView.getSettings().setJavaScriptEnabled(true);
        // 设置可以支持缩放
        mWebView.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        mWebView.getSettings().setBuiltInZoomControls(true);
        //扩大比例的缩放
        mWebView.getSettings().setUseWideViewPort(true);
        //自适应屏幕
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.getSettings().setLoadWithOverviewMode(true);

        //如果不设置WebViewClient，请求会跳转系统浏览器
        mWebView.setWebViewClient(webViewClient);
        mWebView.loadUrl(mUrl);
    }

    @Override
    protected void initData() {
        super.initData();
        mUrl = getIntent().getStringExtra("url");
        mTitle = getIntent().getStringExtra("title");
        if (!mUrl.startsWith("http://") && !mUrl.startsWith("https://")) {
            mUrl = "http://" + mUrl;
        }
    }

    boolean isRedirect = false; //是否是重定向
    boolean isPageOk = false; //目的地地址加载完成

    protected WebViewClient webViewClient = new WebViewClient() {

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            dismissLoadingDialog();
            handler.proceed();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            isRedirect = true;
            isPageOk = false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            dismissLoadingDialog();
            isPageOk = isRedirect;
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            isRedirect = false;
            if (isPageOk) {
                view.loadUrl(url);
                return true;
            }
            return false;
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (mWebView!=null&&keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        } else {
            onBackPressed();
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle(mTitle);
        mWebView = findViewById(R.id.web_view);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mWebView = null;
    }

}
